<template>
  <editNavBar></editNavBar>
  <van-address-edit
    :area-list="areaList"
    :address-info="addressData"
    show-set-default
    show-delete
    :area-columns-placeholder="['请选择', '请选择', '请选择']"
    @save="onSave"
    @delete="onDelete"
  />
</template>

<script setup>
import { ref, reactive } from "vue";
import { areaList } from "@vant/area-data";
import request from "../utils/request.js";
import { useRoute, useRouter } from "vue-router";
import editNavBar from "@/components/editNavBar.vue";
const userId = localStorage.getItem("userId");
const route = useRoute();
const router = useRouter();
// 接收传过来的地址
const data = JSON.parse(route.params.data);
// 把传过来的地址给一个变量
let addressData = ref({
  name: data.real_name,
  tel: data.mobile,
  areaCode: data.area,
  addressDetail: data.house,
  isDefault:data.is_default?true:false
});
// 发送修改地址的请求，content.是传过来的地址信息，修改完之后更新
const onSave = async (content) => {
  const result = await request.post("/api/v1/updateAddress/" + userId, {
    id: data.id,
    real_name: content.name,
    mobile: content.tel,
    city: content.province + content.city + content.county,
    area: content.areaCode,
    house: content.addressDetail,
    is_default:content.isDefault?1:0
  });
  history.back();
};
const onDelete = async(content) => {
const result = await request.post('api/v1/delAddress/'+ userId,{id:data.id})
router.push('/manageradr')
};
</script>

<style lang="scss" scoped></style>
